class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int i = 0;
        int j = i + 1;
        int count = 0;
        while (j < nums.size())
        {
            if (nums[i] == nums[j])
            {
                if (count == 0)
                {
                    nums[++i] = nums[j++];
                }
                if (count > 0)
                {
                    j++;
                }
                count++;
            }
            else
            {
                nums[++i] = nums[j++];
                count = 0;
            }
        }
        return i + 1;
    }
};